package com.wy.wuye.controller;

import com.alibaba.fastjson.JSON;
import com.wy.wuye.IpConfig;
import com.wy.wuye.db.DBLg;
import com.wy.wuye.javabean.Repo;
import com.wy.wuye.javabean.User;
import com.wy.wuye.minio.MinioUtil;
import com.wy.wuye.utils.KeyValue;
import com.wy.wuye.utils.NestTools;
import com.wy.wuye.utils.RepoFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    MinioUtil minioUtil;
    @Autowired
    IpConfig ip;


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@RequestParam("name") String name, @RequestParam("pwd") String pwd) {
        User user = DBLg.getUserForPhone(name, pwd, jdbcTemplate);
        String result = null;
        if (user != null) {
            result = JSON.toJSONString(RepoFactory.createRepo(user, "登录成功", 1));
        } else {
            result = JSON.toJSONString(RepoFactory.createFileRepo("登录失败", -1));
        }
        return result;
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestParam("email") String email, @RequestParam("pwd") String pwd, @RequestParam("phone") String phone,
                           @RequestParam("vcode") String vcode) {
        String result;
        if (vcode.equals("000000")) {
            User dbUser = DBLg.hasRegister(phone, email, jdbcTemplate);
            if (dbUser != null) {
                return JSON.toJSONString(RepoFactory.createFileRepo("你已经注册过了", -1));
            }
            User user = DBLg.insertUser(pwd, email, phone, jdbcTemplate);
            if (user != null) {
                result = RepoFactory.creatRepoString("注册成功", 1, new KeyValue("name", user.getName()), new KeyValue("token", NestTools.KL(user.getToken())), new KeyValue("createtime", user.getCreatetime()));
            } else {
                Repo<Object> repo = RepoFactory.createFileRepo("服务器异常", -1);
                result = JSON.toJSONString(repo);
            }
        } else {
            Repo repo = RepoFactory.createFileRepo("验证码错误", -1);
            result = JSON.toJSONString(repo);
        }

        return result;
    }


    @RequestMapping(value = "/check/login", method = RequestMethod.POST)
    public String loginByToken(@RequestHeader("token") String token) {
        User user = DBLg.getUserForToken(token, jdbcTemplate);
        String result = null;
        if (user != null) {
            result = JSON.toJSONString(RepoFactory.createRepo(user, "登录成功", 1));
        } else {
            result = JSON.toJSONString(RepoFactory.createFileRepo("登录失败", -1));
        }
        return result;
    }

    @RequestMapping(value = "/upload/head/photo", method = RequestMethod.POST)
    @ResponseBody
    public String uploadHeadPhoto(@RequestParam("file") MultipartFile file, @RequestHeader("token") String token) {
        User user = DBLg.getUserForToken(token, jdbcTemplate);
        if (user == null) {
            return JSON.toJSONString(RepoFactory.createFileRepo("请登陆后上传头像", -1));
        }
        try {
            String name = NestTools.MD5(user.getCreatetime()) + ".png";
            minioUtil.putObject("wuye", name, file.getInputStream());
            user.setPicture(ip.getPictureIp() + "/" + name);
            boolean flag = DBLg.updatePicture(user, token, jdbcTemplate);
            if (flag) {
                return RepoFactory.creatRepoString("用户头像设置成功", 1, new KeyValue("picture", user.getPicture()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(RepoFactory.createFileRepo("用户头像设置失败", -1));
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String getUserInfo(@RequestHeader("token") String token) {
        User user = DBLg.getUser(token, jdbcTemplate);
        if (user == null) {
            return JSON.toJSONString(RepoFactory.createFileRepo("请登陆后上传头像", -1));
        }
        return JSON.toJSONString(RepoFactory.createRepo(user, "获取用户信息", 1));
    }

}
